package com.civ.sensor.dht.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
	public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }
	public static boolean isNotBlank(String str) {
        return !StringUtils.isBlank(str);
    }
	public static Date parseStrint2Date(String dateStr, String pattern) {
		Date date = null;
		if (StringUtils.isNotBlank(dateStr)) {
			DateFormat df = new SimpleDateFormat(pattern);
			try {
				date = df.parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}
	
	public static String parseDate2String(Date date, String pattern) {
		String dateStr = "";
		if (date != null) {
			DateFormat df = new SimpleDateFormat(pattern);
			dateStr = df.format(date);
		}
		return dateStr;
	}
	
	public static String appendTimeFrom(String dateStr) {
		String result = " 00:00:00.0";
		if (StringUtils.isNotBlank(dateStr)) {
			return dateStr + result;
		}
		return "";
	}
	public static String appendTimeFromSecond(String dateStr) {
		String result = " 00:00:00";
		if (StringUtils.isNotBlank(dateStr)) {
			return dateStr + result;
		}
		return "";
	}
	
	public static String appendTimeTo(String dateStr) {
		String result = " 23:59:59.999";
		if (StringUtils.isNotBlank(dateStr)) {
			return dateStr + result;
		}
		return "";
	}
	
	public static String appendTimeToSecond(String dateStr) {
		String result = " 23:59:59";
		if (StringUtils.isNotBlank(dateStr)) {
			return dateStr + result;
		}
		return "";
	}
	
	/**
	 * @param dateStr yyyy-MM-dd鏍煎紡鐨勬棩鏈?
	 * @return
	 */
	public static Date parseStrint2DateFrom(String dateStr) {
		Date date = null;
		if (StringUtils.isNotBlank(dateStr)) {
			dateStr = appendTimeFrom(dateStr);
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
			try {
				date = df.parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}
	
	/**
	 * @param dateStr yyyy-MM-dd鏍煎紡鐨勬棩鏈?
	 * @return
	 */
	public static Date parseStrint2DateTo(String dateStr) {
		Date date = null;
		if (StringUtils.isNotBlank(dateStr)) {
			dateStr = appendTimeTo(dateStr);
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
			try {
				date = df.parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}
	/**
	 * @param dateStr yyyy-MM-dd鏍煎紡鐨勬棩鏈?
	 * @return
	 */
	public static Date parseStringformatDate(String dateStr,String format) {
		Date date = null;
		if (StringUtils.isNotBlank(dateStr)) {
			dateStr = appendTimeTo(dateStr);
			DateFormat df = new SimpleDateFormat(format);
			try {
				date = df.parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}
	
	/**
	 * @param dateStr 鎸囧畾骞存湀鏃ユ牸寮?
	 * @return
	 */
	public static Date parseStrint2DateTo(String dateStr, String yyMMdd) {
		Date date = null;
		if (StringUtils.isNotBlank(dateStr)) {
			dateStr = appendTimeTo(dateStr);
			DateFormat df = new SimpleDateFormat(yyMMdd+" HH:mm:ss.S");
			try {
				date = df.parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}

    /**
     * 杩斿洖褰撳勾鐨勬渶澶ф椂闂?
     * @return
     */
   public static Date getCurrentMaxYear(){
       SimpleDateFormat all_sdf = new SimpleDateFormat("yyyyMMddHHmmss");
       try {
           return all_sdf.parse(getCurrentMaxYearStr());
       } catch (ParseException e) {
           e.printStackTrace();
       }
       return null;
   }

    /**
     * 杩斿洖褰撳勾鐨勬渶澶ф椂闂?
     * @return
     */
    public static String getCurrentMaxYearStr(){
        Date now = getCurrentDateTime();
        SimpleDateFormat year_sdf = new SimpleDateFormat("yyyy");
        String year_str = year_sdf.format(now);
        return year_str + "1231235959";
    }

    /**
     * 杩斿洖褰撳勾鐨勬渶灏忔椂闂?
     * @return
     */
    public static Date getCurrentMinYear(){
        SimpleDateFormat all_sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            return all_sdf.parse(getCurrentMinYearStr());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 杩斿洖褰撳勾鐨勬渶灏忔椂闂?
     * @return
     */
    public static String getCurrentMinYearStr(){
        Date now = getCurrentDateTime();
        SimpleDateFormat year_sdf = new SimpleDateFormat("yyyy");
        String year_str = year_sdf.format(now);
        return year_str + "0101000000";
    }

    /**
     * 杩斿洖褰撴湀鐨勬渶灏忔椂闂?
     * @return
     */
    public static Date getCurrentMinMonth(){
        SimpleDateFormat all_sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            return all_sdf.parse(getCurrentMinMonthStr());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 杩斿洖褰撴湀鐨勬渶灏忔椂闂?
     * @return
     */
    public static String getCurrentMinMonthStr(){
        Date now = getCurrentDateTime();
        SimpleDateFormat mon_sdf = new SimpleDateFormat("yyyyMM");
        String mon_str = mon_sdf.format(now);
        return mon_str + "01000000";
    }

    /**
     * 杩斿洖褰撴湀鐨勬渶澶ф椂闂?
     * @return
     */
    public static Date getCurrentMaxMonth(){
        SimpleDateFormat all_sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            return all_sdf.parse(getCurrentMaxMonthStr());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 杩斿洖褰撴湀鐨勬渶澶ф椂闂?
     * @return
     */
    public static String getCurrentMaxMonthStr(){
        Calendar c = Calendar.getInstance();
        Date now = getCurrentDateTime();
        SimpleDateFormat mon_sdf = new SimpleDateFormat("yyyyMM");
        String mon_str = mon_sdf.format(now);
        int day_max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        return mon_str + day_max+"235959";
    }

    /**
     * 杩斿洖褰撳ぉ鐨勬渶澶ф椂闂?
     * @return
     */
    public static Date getCurrentMaxDay(){
        SimpleDateFormat all_sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            return all_sdf.parse(getCurrentMaxDayStr());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 杩斿洖褰撳ぉ鐨勬渶澶ф椂闂?
     * @return
     */
    public static String getCurrentMaxDayStr(){
        Date now = getCurrentDateTime();
        SimpleDateFormat day_sdf = new SimpleDateFormat("yyyyMMdd");
        String day_str = day_sdf.format(now);
        return day_str + "235959";
    }

    /**
     * 杩斿洖褰撳ぉ鐨勬渶灏忔椂闂?
     * @return
     */
    public static Date getCurrentMinDay(){
        SimpleDateFormat all_sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            return all_sdf.parse(getCurrentMinDayStr());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 杩斿洖褰撳ぉ鐨勬渶灏忔椂闂?
     * @return
     */
    public static String getCurrentMinDayStr(){
        Date now = getCurrentDateTime();
        SimpleDateFormat day_sdf = new SimpleDateFormat("yyyyMMdd");
        String day_str = day_sdf.format(now);
        return day_str + "000000";
    }
	
	/**
	 * 鑾峰彇鎸囧畾鏃堕棿娈电殑鏃ユ湡鍒楄〃
	 * @param dateFrom
	 * @param dateTo
	 * @param pattern
	 * @return
	 */
	public static List<String> getDateRangeList(Date dateFrom, Date dateTo, String pattern) {
		List<String> dateRangeList = new ArrayList<String>();
		Date date = dateFrom;
		Calendar calendar = Calendar.getInstance();
		while (date.compareTo(dateTo) <= 0) {
			dateRangeList.add(parseDate2String(date, pattern));
			calendar.setTime(date);
			calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)+1);
			date = calendar.getTime();
		}
		return dateRangeList;
	}

	/**
	 * 鑾峰彇褰撳墠搴旂敤鏈嶅姟鍣ㄧ殑绯荤粺鏃ユ湡鏃堕棿
	 * @return
	 */
	public static Date getCurrentDateTime(){
		return new Date(System.currentTimeMillis());
	}


	/**
	 * 鑾峰彇褰撳墠搴旂敤鏈嶅姟鍣ㄧ殑绯荤粺鏃ユ湡鏃堕棿
	 * @return 鏃ユ湡鏃堕棿瀛楃涓诧紝鏍煎紡锛歽yyy-MM-dd HH:mm:ss
	 */
	public static String getCurrentDateTimeStr(){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(new Date(System.currentTimeMillis()));
	}
	
	public static String getCurrentDateFormatStr(String format){
		DateFormat df = new SimpleDateFormat(format);
		return df.format(new Date(System.currentTimeMillis()));
	}
	/**
	 * 鑾峰彇褰撳墠搴旂敤鏈嶅姟鍣ㄧ殑绯荤粺鏃ユ湡鏃堕棿
	 * @return 鏃ユ湡鏃堕棿瀛楃涓诧紝鏍煎紡锛歽yyyMMddHHmmss
	 */
	public static String getCurrentDateTime14(){
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(new Date(System.currentTimeMillis()));
	}
	/**
	 * 鑾峰彇褰撳墠搴旂敤鏈嶅姟鍣ㄧ殑绯荤粺鏃ユ湡鏃堕棿
	 * @return 鏃ユ湡鏃堕棿瀛楃涓诧紝鏍煎紡锛歽yyyMMdd
	 */
	public static String getCurrentDate8(){
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(new Date(System.currentTimeMillis()));
	}
	public static String getFormatString(String date,String srcpattern,String destpattern){
		String retstr="";
		try {
			Date d = new SimpleDateFormat(srcpattern).parse(date);
			retstr = new SimpleDateFormat(destpattern).format(d);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retstr;
	}
	
	/**
	 * 澧炲姞鎴栧噺灏戞椂闂?
	 * @param date
	 * @param calendarField 澧炲姞浣滅敤鍩熸槧灏勫??   濡傦細骞翠唤涓? Calendar.YEAR = 1
	 * @param amount  姝ｆ暟涓哄鍔狅紝璐熸暟涓哄噺灏?   
	 * @return
	 */
	public static Date addDate(Date date, int calendarField, int amount){
		if(null == date){
			return date;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		//calendar.roll(calendarField, amount);
		calendar.add(calendarField, amount);
		return calendar.getTime();
		
	}
	
	public static void main(String[] args){
        System.out.println(getCurrentMaxMonth());
        System.out.println(getCurrentMaxMonthStr());

		System.out.println(addDate(new Date(), Calendar.MINUTE, -10));
	}
}
